﻿// a global variable

//var users=[];
var zone;
var dbase
var gameRooms;

//var myLocalUserList = {};

function init()
{
	trace("Initing dbExtension")

	//初始化扩展的时候，将当前的zone的引用保存起来
	zone = _server.getCurrentZone()
	
	gameRooms = zone.getRooms()

	// get a reference to the database manager object
	// This will let you interact the database configure for this zone
	dbase = _server.getDatabaseManager()

}

function destroy()
{
	// Release the reference to the dbase manager
	delete dbase
}


function handleRequest(cmd, params, user, fromRoom)
{
	// 获取消息发送在房间里的 所有user
	//users=zone.getRoom(fromRoom).getAllUsers();
	//根据不同的指令 进行不同的操作
	switch(cmd){
		
	}

}


function handleInternalEvent(evt)
{
	trace("Event received: " + evt.name)
	
	if (evt.name == "loginRequest")
	{
		var error = ""
		
		var nick = evt["nick"]
		var pass = evt["pass"]
		var chan = evt["chan"]

		var response = new Object()

		var t1 = getTimer()
		
		var sql = "select * from users where userNumber=" + nick ;
		var queryRes = dbase.executeQuery(sql)
		
		var t2 = getTimer()
		
		trace("DB Request took: " + (t2-t1) + " ms.")
		
		if (queryRes != null)
		{	
			
			var tempRow = queryRes.get(0)
			var userNumber = tempRow.getItem("userNumber")
			var password = tempRow.getItem("password")
	
			if( nick==userNumber && pass==password){
				
				response.err="成功登入！"
				
				var obj = _server.loginUser(nick, pass, chan)
				
				if (obj.success == false){
					response.err="请先退出后再登入。"
				}else{
					var u = _server.instance.getUserByChannel(chan)
					trace("User " + u.getName() + " logged in")
					response._cmd = "logOK"
					response.myUserId=u.getUserId();
					response.userNumber=u.getName();
					response.nickname=tempRow.getItem("nickname");
					response.username = tempRow.getItem("username");
					response.sex = tempRow.getItem("sex");
					response.department = tempRow.getItem("department");
					response.userClass = tempRow.getItem("class");
					response.PVcoins = tempRow.getItem("PVcoins");
					response.level = tempRow.getItem("level");
					response.exp = tempRow.getItem("exp");
					response.pic = tempRow.getItem("pic");
					response.clothes = tempRow.getItem("clothes");
					response.association = tempRow.getItem("association");
					response.qq = tempRow.getItem("qq");
					response.phoneNumber = tempRow.getItem("phoneNumber");
					response.birthday = tempRow.getItem("birthday");
					response.province = tempRow.getItem("province");
					response.offlineMessage = tempRow.getItem("offlineMessage");
					response.onlineTime = tempRow.getItem("onlineTime");
					response.messageBoard = tempRow.getItem("messageBoard");
					response.buddyList = tempRow.getItem("buddyList");
					response.lastMapId = tempRow.getItem("lastMapId");
					response.lastX = tempRow.getItem("lastX");
					response.lastY = tempRow.getItem("lastY");
				}
				
			}
			else
			{
				response._cmd = "logKO"
				response.err = "密码或用户名错误，请重新输入！"
			}
			
		}
		else
		{
			response._cmd = "logKO"
			response.err = "密码或用户名错误，请重新输入！"
			trace("DB Query failed")
		}
		
		_server.sendResponse(response, -1, null, chan)
		
		trace(response._cmd);

	}
	
	if (evt.name == "userJoin")
	{
		var room_join = evt.room
		var user_join = evt.user
		
		//myLocalUserList[user_join.getId] = user_join;
		
		trace("User: " + user_join.getName() + " joined room: " + room_join.getName())
	}
	
	if (evt.name == "userExit")
	{
		var exit_room = evt.room
		var exit_uid = evt.userId
		//var exit_userList = exit_room.getAllUsers()
		
		//trace("User: " + exit_user.getName() + "left the room")
		
		//delete exit_userList[exit_uid]
		
		// Get the user from the local list
		//var uExit = myLocalUserList[uid_exit]
		
		// Remove the user from the local list
		//delete myLocalUserList[uid_exit]
	}
	
	if (evt.name == "userLost")
	{
		//var lost_uid = evt.userId						// id of the user that was lost
		//var roomIds = evt.roomIds				// list of room id(s) where user was present
		//var playerIndexes = evt.playerIndexs		// list of playerIndex(es) for each room
		
		// Get the roomId
		//var rId = roomIds[0]						// Our app does not allow users to be present in more 
		// than one room at a time!
		// Get the playerIndex
		//playerIndex = playerIndexes[0]
		
		// Get the game room
		//var gameRoom = gameRooms[rId]
			
		//var lost_userList = gameRoom.getAllUsers();
		
		// Get the user that was lost
		//var lostUser = gameRoom.userList[uid_lost]
		
		//trace("User: " + lostUser.getName())
		
		// Remove user from list
		//delete lost_userList[lost_uid]
		
		//
		// Here goes the code that handles the game logic...
		// ...
		//
		
		/*if (playerIndex == -1)
		{
			// User was a spectator ...
		}
		else if (playerIndex > 0)
		{
			// User was one of the players in this room ...
		}*/
	}

	
}